//  Depends: jQuery
//  Depends: namespace.js
//  Depends: modal.js
//  Depends: settings.js
//  Depends: bookdb.js (database-websql.js)

var Bistre = Bistre || {};
var Bistre.MobileBookCatalog = Bistre.MobileBookCatalog || {};

Bistre.MobileBookCatalog.Core = (function() {
    Core: function(settings) {
		var pub = {};	// Anything added to this object will be public, anything else is private
		
		// -------------------------------------------------------------------------------------------
		// Private variables
		// -------------------------------------------------------------------------------------------
		var _settings = settings;
		var _bookDb = null;
		var $_initList = null;
		
		// -------------------------------------------------------------------------------------------
		// Private Functions
		// -------------------------------------------------------------------------------------------
		function loadingStatus(text, value)
		{
		  return '<li>'+ text + '<span class="ui-li-count">' + value + '</span></li>';
		}
		
		function SetupDatabase() {
			// Build Init Queue
			_initQueue = $.Deferred().done(function() {
			}).done(function() {
			// Then init BookDB (websql version)
				_bookDb = new BookDatabaseWebSql();
				_bookDb.init();
			}).fail(function() {
				$_initList.append(loadingStatus('Init Database', 'fail'));
			}).done(function() {
				$_initList.append(loadingStatus('Init Database', 'ok'));
			if (_settings.lastImportTime == '') {
				_initQueue.reject();
			} else {
				_initQueue.resolve();
			}
			}).done(function() {
				$_initList.append(loadingStatus('Check for Book XML', 'ok'));
			}).fail(function() {
				$_initList.append(loadingStatus('Check for Book XML', 'loading'));
				ImportXml();
			}).done(function() {
				$_initList.append(loadingStatus('Import XML', 'ok'));
			}).fail(function() {
				$_initList.append(loadingStatus('Import XML', 'fail'));
			});
		}
		
		function ImportXml()
		{
		  //TODO: Need to check if we need to import, this will be done by checking if teh last import fields are set
		  
		  if (_settings.importType == 'calibre-xml') {
			$.ajax({
			  type: "GET",
			  url: _settings.importUrl,
			  dataType: "xml",
			  success: function(data, textStatus, jqXHR)
			  {
				_bookDb.clear();  // Wipe database
				$(data).find('record').each(function() {
				  var book = new Book();
				  book.uuid = $(this).find('uuid').text();
				  book.publisher = $(this).find('publisher').text();
				  book.rating = $(this).find('rating').text();
				  book.isbn = $(this).find('isbn').text();
				  book.title = $(this).find('title').text();
				  $(this).find('authors author').each(function() { book.authors.push($(this).text()) }); 
				  $(this).find('tags tag').each(function() { book.tags.push($(this).text()) }); 
				  book.comments = $(this).find('comments').text();
				  book.series = $(this).find('series').text();
				  book.seriesIndex = $(this).find('series').attr('index');

				  _bookDb.savebook(book);
				});
			  },
			  error: function(jqXHR, textStatus, errorThrown)
			{
			  alert('Error Importing URL. Please check Options to insure they are correct and reload if needed.')
			},
			complete: function(jqXHR, textStatus)
			{
			  alert('Done importing URL.');
			}
			});
		  } 
		  else {
			alert('ERROR: Unknown Import Type.')
		  }
		}
	
		
		// -------------------------------------------------------------------------------------------
		// Public functions
		// -------------------------------------------------------------------------------------------
		pub.init = function(settings) { 
			$_initList = $('div#loading ul');
		}
		
		return pub;
	}
}());